import storage from '@/utils/localStorage'
export default{
  // 定位
  locationCity ({commit}, city) {
    commit('LOCATION_CITY', city)
  },
  // 切换地市
  toggleCity ({commit}, city) {
    storage.set('currentCity', city)
    let historyCities = storage.get('historyCities')
    if (!historyCities) {
      historyCities = []
    }
    const flag = historyCities.some(item => {
      return item.cname === city.cname
    })
    if (!flag) {
      let historyStr = JSON.stringify(historyCities)
      const cityStr = '[' + JSON.stringify(city) + (historyCities.length === 0 ? '' : ',')
      const citiesStr = historyStr.replace(/\[/, cityStr)
      historyCities = JSON.parse(citiesStr)
      historyCities = historyCities.slice(0, 3)
      storage.set('historyCities', historyCities)
    }
    commit('TOGGLE_CITY', city)
  },
  toggleTab ({commit}, path) {
    let currentTab = {}
    if (path.indexOf('film') > -1)currentTab = {tabUrl: 'film', title: '猫眼电影'}
    if (path.indexOf('cinema') > -1)currentTab = {tabUrl: 'cinema', title: '影院'}
    if (path.indexOf('mine') > -1)currentTab = {tabUrl: 'mine', title: '我的'}
    commit('TOGGLE_TAB', currentTab)
  }
}
